﻿using System;
using System.IO;
using BizTalk.Archiving.Common;
using BizTalk.Archiving.Common.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTest
{
    
    
    /// <summary>
    ///This is a test class for MessageArchiverTest and is intended
    ///to contain all MessageArchiverTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MessageArchiverTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for ArchiveToDb
        ///</summary>
        [TestMethod()]
        public void ArchiveToDbTest_NotZipped()
        {
            MessageArchiver target = new MessageArchiver();
            string connectionString = "Data Source=localhost;Initial Catalog=BizTalkArchiveDb;Integrated Security=SSPI";
            string providerName = "System.Data.SqlClient";
            string storedProcName = "InsMessages";
            Guid msgId = Guid.NewGuid();
            Stream msgStream = File.Open("Sample3MBFile.txt", FileMode.Open);
            string xmlProperties = @"<MessageProperties><Test>123</Test></MessageProperties>";

            string zipPassword = string.Empty; 
            bool expected = true;
            bool actual;
            actual = target.ArchiveToDb(connectionString, providerName, storedProcName, msgId, msgStream, xmlProperties, msgStream.Length, false,0);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ArchiveToDb
        ///</summary>
        [TestMethod()]
        public void ArchiveToDbTest_Zipped()
        {
            MessageArchiver target = new MessageArchiver();
            string connectionString = "Data Source=localhost;Initial Catalog=BizTalkArchiveDb;Integrated Security=SSPI";
            string providerName = "System.Data.SqlClient";
            string storedProcName = "InsMessages";
            Guid msgId = Guid.NewGuid();
            long length = 0;
            using (Stream msgStream = File.Open("Sample3MBFile.txt", FileMode.Open))
            {
                length = msgStream.Length;
                string xmlProperties = @"<MessageProperties><Test>123</Test></MessageProperties>";
                
                string zipPassword = string.Empty;
                bool expected = true;
                bool actual;
                Stream zippedStream;
                zippedStream = Utility.GetZipStream("Sample3MBFile.txt", msgStream, "");
                actual = target.ArchiveToDb(connectionString, providerName, storedProcName, msgId, zippedStream, xmlProperties, length, true, zippedStream.Length);
                Assert.AreEqual(expected, actual);
            }

        }


    }
}
